package at.ac.univie.zsu.aguataplan.util;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.log4j.Logger;

/**
 * @author gerry
 * 
 */
public class EnvironmentHandler {
	private static Logger log = Logger.getLogger(EnvironmentHandler.class);

	public static final String APP = "AGUATAPLAN";

	public static final String MVN_RUN = "m";
	public static final String MVN_RUN_DESC = "Maven run config";
	public static final String BATCH_RUN = "b";
	public static final String BATCH_RUN_DESC = "Batch run config";

	public static final int BATCH = 0;
	public static final int MVN = 1;

	private static int type;

	public static void readArgs(String[] args) {

		Options options = new Options();

		options.addOption(MVN_RUN, false, MVN_RUN_DESC);
		options.addOption(BATCH_RUN, false, BATCH_RUN_DESC);

		HelpFormatter formatter = new HelpFormatter();

		CommandLineParser parser = new BasicParser();
		CommandLine cmd = null;

		try {
			cmd = parser.parse(options, args, true);
		} catch (ParseException e) {
			log.fatal("could not parse arguments");
			System.exit(-1);
		}

		if (cmd.hasOption(MVN_RUN)) {
			type = MVN;
			return;
		}

		if (cmd.hasOption(BATCH_RUN)) {
			type = BATCH;
			return;
		}

		formatter.printHelp(APP, options);
		System.exit(-1);

	}

	public static String adaptPath(String location) {
		String s = location;
		if (type == MVN) {
			s = "target/classes/" + location;
		} else if (type == BATCH) {
			s = location;

		}
		return s;
	}

}
